﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports System.Xml
Imports System.IO
Imports System.Globalization

Namespace Infrastructure

    Public NotInheritable Class StorageRequestAnalyzer
        Private Sub New()
        End Sub
        Public Shared Function GetRequestedTable(ByVal request As HttpRequest) As String
            Dim path = request.PathInfo
            If path.ToUpperInvariant().Contains("/TABLES(") Then
                Dim match = Regex.Match(path, "tables\(['""](\w+)['""]\)", RegexOptions.IgnoreCase)
                If match.Groups.Count > 1 Then
                    Return match.Groups(1).Value
                End If
            Else
                Dim slashPos = path.LastIndexOf("/", StringComparison.OrdinalIgnoreCase)
                Dim parenthesisPos = path.IndexOf("(", StringComparison.OrdinalIgnoreCase)
                parenthesisPos = If(parenthesisPos = -1, path.Length - 1, parenthesisPos - 1)

                If slashPos > -1 Then
                    Return path.Substring(slashPos + 1, parenthesisPos - slashPos)
                Else
                    Return path
                End If
            End If

            Return String.Empty
        End Function

        Public Shared Function GetTableToCreate(ByVal request As HttpRequest) As String
            Dim tableName = String.Empty
            Dim buffer = New Byte(CInt(request.InputStream.Length - 1)) {}

            request.InputStream.Seek(0, SeekOrigin.Begin)
            request.InputStream.Read(buffer, 0, CInt(request.InputStream.Length))
            request.InputStream.Seek(0, SeekOrigin.Begin)

            Dim xml = New XmlDocument()
            xml.LoadXml(ASCIIEncoding.ASCII.GetString(buffer))

            Dim tableElement = xml.GetElementsByTagName("d:TableName")
            If tableElement.Count > 0 Then
                tableName = tableElement(0).InnerText
            End If

            Return tableName
        End Function

        Public Shared Function GetRequestedQueue(ByVal request As HttpRequest) As String
            Dim queueName = request.PathInfo.TrimStart("/"c)
            Dim slashPost = queueName.IndexOf("/"c)

            Return If(slashPost > 0, queueName.Remove(slashPost), queueName)
        End Function

        Public Shared Function IsListingTables(ByVal request As HttpRequest) As Boolean
            If request.PathInfo.Equals("/Tables", StringComparison.OrdinalIgnoreCase) AndAlso request.HttpMethod.Equals("GET", StringComparison.OrdinalIgnoreCase) AndAlso request.ContentLength <= 0 Then
                Return True
            End If

            Return False
        End Function

        Public Shared Function IsCreatingTable(ByVal request As HttpRequest, ByVal tableName As String) As Boolean
            If tableName.Equals("Tables", StringComparison.OrdinalIgnoreCase) AndAlso request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase) Then
                Return True
            End If

            Return False
        End Function

        Public Shared Function IsCreatingQueue(ByVal request As HttpRequest) As Boolean
            If (Not request.PathInfo.TrimStart("/"c).Contains("/")) AndAlso request.HttpMethod.Equals("PUT", StringComparison.OrdinalIgnoreCase) AndAlso request.ContentLength <= 0 Then
                Return True
            End If

            Return False
        End Function

        Public Shared Function IsDeletingTable(ByVal request As HttpRequest, ByVal tableName As String) As Boolean
            Dim requestPath = String.Format(CultureInfo.InvariantCulture, "/Tables('{0}')", tableName)
            If request.PathInfo.Equals(requestPath, StringComparison.OrdinalIgnoreCase) AndAlso (request.HttpMethod.Equals("DELETE", StringComparison.OrdinalIgnoreCase) OrElse request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase)) AndAlso request.ContentLength <= 0 Then
                Return True
            End If

            Return False
        End Function

        Public Shared Function IsDeletingQueue(ByVal request As HttpRequest) As Boolean
            If (Not request.PathInfo.TrimStart("/"c).Contains("/")) AndAlso request.HttpMethod.Equals("DELETE", StringComparison.OrdinalIgnoreCase) AndAlso request.ContentLength <= 0 Then
                Return True
            End If

            Return False
        End Function
    End Class
End Namespace